'''
Author:Shen.Pengxu/Lepus
Date:2022年03月21日
'''

import requests
import datetime
import urllib.request
import re

year = datetime.datetime.now().year
month = datetime.datetime.now().month
day = datetime.datetime.now().day
leap_year = False
year_dict = {}
total_day = 0
what_day_now = 0
char_what_day = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]

bing_pic_url = ""

next_salary = 0
next_weekend = 0
next_holiday = 0

# 获取每日 bing 图片 picurl
def get_bing_backphoto():
    global bing_pic_url
    url = 'http://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&nc=1361089515117&FORM=HYLH1'
    html = urllib.request.urlopen(url).read()

    if html == 'null':
        print('open & read bing error!')
        sys.exit(-1)

    html = html.decode('utf-8')
    html = html.replace('/az/','http://cn.bing.com/az/')
    reg = re.compile('"url":"(.*?)","urlbase"',re.S)
    bing_pic_url = str(re.findall(reg,html))
    bing_pic_url = "http://cn.bing.com"+ bing_pic_url[2:-2]

# if it is leap year
if year % 100 == 0:
    if year % 400 == 0:
        leap_year = True
    else:
        leap_year = False
else:
    if year % 4 == 0:
        leap_year = True
    else:
        leap_year = False

# creat year dictionary
if leap_year == True:
    year_dict = {0 : 0, 1 : 31, 2 : 29, 3 : 31, 4 : 30, 5 : 31, 6 : 30, 7 : 31, 8 : 31, 9 : 30, 10 : 31, 11 : 30, 12 : 31}
else:
    year_dict = {0 : 0, 1 : 31, 2 : 28, 3 : 31, 4 : 30, 5 : 31, 6 : 30, 7 : 31, 8 : 31, 9 : 30, 10 : 31, 11 : 30, 12 : 31}

# calculate next salary day
if day < 22:
    next_salary = "<font color=\"comment\">工资还有</font>**<font color=\"warning\"> "+ str(22 - day) +"</font>** <font color=\"comment\">天发！</font>\n"
elif day > 22:
    next_salary = "<font color=\"comment\">工资还有</font>**<font color=\"warning\"> "+ str(year_dict[month] - (day - 22)) +"</font>** <font color=\"comment\">天发！</font>\n"
else:
    next_salary = "**<font color=\"warning\">千金散尽还复来！</font>**\n"

#calculate next weekend
for i in range(month):
    total_day += year_dict[i]
total_day += day
what_day_now = (total_day + 5) % 7
if what_day_now == 6 or what_day_now == 0:
    next_weekend = "**<font color=\"info\">尔来闲静日，周末正当值。</font>**\n"
else :
    next_weekend = "<font color=\"comment\">距离两天小长假还有</font> **<font color=\"warning\">"+ str(6 - what_day_now) +"</font>** <font color=\"comment\">天</font>\n"

#calculate next holiday
if month < 4 :
    next_holiday = "<font color=\"comment\">距离清明小长假还有</font> **<font color=\"warning\">"+ str(year_dict[3] - day + 3) +"</font>** <font color=\"comment\">天</font>\n"
elif month == 4:
    if day < 3:
        next_holiday = "<font color=\"comment\">距离清明小长假还有</font> **<font color=\"warning\">"+ str(3 - day) +"</font>** <font color=\"comment\">天</font>\n"
    elif day >= 3 and day <= 5:
        next_holiday = "<font color=\"warning\">**梨花风起正清明，游子寻春半出城。**</font>\n"
    elif day > 5 and day < 30:
        next_holiday = "<font color=\"comment\">距离五一小长假还有</font> **<font color=\"warning\">"+ str(30 - day) +"</font>** <font color=\"comment\">天</font>\n"
    else:
        next_holiday = "**<font color=\"warning\">真挚而纯洁的爱情，一定渗有对心爱的人的劳动和职业的尊重。</font>**\n"
elif month == 5:
    if day <= 4:
        next_holiday = "**<font color=\"warning\">真挚而纯洁的爱情，一定渗有对心爱的人的劳动和职业的尊重。</font>**\n"
    else:
        next_holiday = "<font color=\"comment\">距离端午小长假还有</font> **<font color=\"warning\">"+ str(year_dict[5] - day + 3) +"</font>** <font color=\"comment\">天</font>\n"
elif month == 6 and day <= 5:
    if day < 3:
        next_holiday = "<font color=\"comment\">距离端午小长假还有</font> **<font color=\"warning\">"+ str(3 - day) +"</font>** <font color=\"comment\">天</font>\n"
    else:
        next_holiday = "**<font color=\"warning\">彩线轻缠红玉臂，小符斜挂绿云鬟。佳人相见一千年。</font>**\n"
elif (month == 6 and day > 5) or (month > 6 and month < 9):
    for i in range(month + 1, 9):
        next_holiday += year_dict[i]
    next_holiday = "<font color=\"comment\">距离中秋佳节还有</font> **<font color=\"warning\">"+ str(year_dict[month] - day + next_holiday + 10) +"</font>** <font color=\"comment\">天</font>\n"
elif month == 9:
    if day < 10:
        next_holiday = "<font color=\"comment\">距离中秋佳节还有</font> **<font color=\"warning\">"+ str(10 - day) +"</font>** <font color=\"comment\">天</font>\n"
    elif day >= 10 and day <= 12:
        next_holiday = "**<font color=\"warning\">但愿人长久，千里共婵娟。</font>**\n"
    else:
        next_holiday = "<font color=\"comment\">距离十一长假还有</font> **<font color=\"warning\">"+ str(30 - day + 1) +"</font>** <font color=\"comment\">天</font>\n"
elif month == 10 and day <= 7:
    next_holiday = "**<font color=\"warning\">为什么我的眼里常含泪水？因为我对这土地爱得深沉……</font>**\n"
else:
    next_holiday = "**<font color=\"warning\">很遗憾，本年度法定节假日已无了！！！</font>**\n"

for i in char_what_day:
    if char_what_day.index(i) == what_day_now:
        what_day_now = i

# Text 模式消息
data_text = {
    "msgtype": "markdown",
    "markdown": {
        "content": "# **"+ str(year) +"年"+ str(month) +"月"+ str(day) +"日\t"+ what_day_now +"**\n"
                    "## **倒计时**\n"
                    ">## **工资**\n"
                    ">"+ next_salary +
                    ">## **周末**\n"
                    ">"+ next_weekend +
                    ">## **假期**\n"
                    ">"+ next_holiday +"\n\n"
                    "<font color=\"comment\">   当前版本不支持调休周末</font>\n"
    }
}

# post 到你的机器人 webhook，注意 url 后填写你自己机器人的专属 webhook
r = requests.post(url='Webhook OF YOURS', json=data_text)
print(r.text)

# News 模式消息
get_bing_backphoto()
print(bing_pic_url, type(bing_pic_url))
data_news = {
    "msgtype":"template_card",
    "template_card":{
        "card_type":"news_notice",
        "source":{
            "icon_url":"https://s2.loli.net/2022/03/22/L2N5ATlPhUzm8EQ.png",
            "desc":"新迪报",
            "desc_color": 0
        },
        "main_title":{
            "title":"# DAiLY 小工具",
            "desc":"DiNews Software Technologies"
        },
        "card_image":{
            "url":bing_pic_url,
            "aspect_ratio":1.78
        },
        "image_text_area":{
            "type":1,
            "url":"https://tianqi.moji.com/weather/china/beijing/tongzhou-district",
            "title":"春有百花秋有月",
            "desc":"夏有凉风冬有雪",
            "image_url":"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.guguzhu.com%2Fd%2Ffile%2Fandroid%2Fico%2F2022%2F02%2F02%2Fqucswoag0y4.png&refer=http%3A%2F%2Fimg.guguzhu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1650527008&t=d596423877492d432bf1db93170f3dd1"
        },
        "quote_area":{
            "type":1,
            "url":"https://gitee.com/lepus57/workwxbot",
            "quote_text":"Version 1.2.324 "
                        "Copyright © 2021-2022  Co., Ltd.\n"
                        "All rights reserved."
        },
        "jump_list": [
            {
                "type": 1,
                "url": "https://w.haoshuo.net/",
                "title": "Wordle"
            },
            {
                "type": 1,
                "url": "http://www.infzm.com/",
                "title": "每日新鲜事"
            }
        ],
        "card_action":{
            "type":1,
            "url":"http://cn.bing.com",
        }
    }
}

# post 到你的机器人 webhook，注意 url 后填写你自己机器人的专属 webhook
r = requests.post(url='Webhook OF YOURS', json=data_news)
print(r.text)